//
//  HJG_YinSi.m
//  YingSiTongYong
//
//  Created by developer on 2018/10/8.
//  Copyright © 2018 developer. All rights reserved.
//

#import "HJGprivacy.h"
#import <UIKit/UIKit.h>
#import "RSA.h"
#define isFirstYinSi  @"isFirstYinSi"


@interface HJGprivacy()<UIWebViewDelegate>

@property(nonatomic,strong)UIWindow * window;

@property (nonatomic, strong) UIView *frontView;

@property (nonatomic, strong) UIWebView *webV;

@property (nonatomic, strong) UIButton *closeBut;

@property (nonatomic, strong) UIWebView *webB;

@end
@implementation HJGprivacy

- (UIWebView *)webV
{
    if (!_webV) {
        UIWebView * theView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 70, [UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.height - 200)];
        theView.delegate = self;
        NSURL* url = [NSURL  URLWithString:@"http://www.footmon.com/AppPrivatePage"];
        [theView loadRequest:[NSURLRequest requestWithURL:url]];
        [self.frontView addSubview:theView];
        _webV = theView;
        
    }
    return _webV;
}

- (UIView *)frontView
{
    if (!_frontView) {
        UIView * theView = [[UIView alloc] init];
        theView.backgroundColor = [UIColor whiteColor];
        theView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        _frontView = theView;
    }
    return _frontView;
}

- (UIButton *)closeBut
{
    if (!_closeBut) {
        UIButton * theView = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.webV.frame)-50, CGRectGetMaxY(self.webV.frame) + 10, 100, 60)];
        [theView setTitle:@"agree" forState:UIControlStateNormal];
        theView.layer.cornerRadius= 30;
        theView.layer.borderColor = [UIColor blackColor].CGColor;
        theView.layer.borderWidth= 2;
        theView.clipsToBounds = YES;
        [theView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [theView addTarget:self action:@selector(closeButClick) forControlEvents:UIControlEventTouchUpInside];
        theView.backgroundColor = [UIColor whiteColor];
        _closeBut = theView;
    }
    return _closeBut;
}

- (void)closeButClick{
    
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:isFirstYinSi];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.frontView removeFromSuperview];
}

+(void)load{
    [self shareManager];
}

+(HJGprivacy *)shareManager{
    static HJGprivacy *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        instance = [[HJGprivacy alloc] init];
    });
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰,当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            [self showPrivatePage];
        }];
    }
    return self;
}


- (void)showPrivatePage {
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:isFirstYinSi] intValue] == 1) {
        UIWebView * theView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        theView.delegate = self;
        NSURL* url = [NSURL  URLWithString:@"http://www.footmon.com/AppPrivatePage"];
        [theView loadRequest:[NSURLRequest requestWithURL:url]];
        _window = [UIApplication sharedApplication].keyWindow;
        [_window addSubview:theView];
        
    }else{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self setupLaunchAd];
        });
    }
}

-(void)setupLaunchAd{
    _window = [UIApplication sharedApplication].keyWindow;
    [_window addSubview:self.frontView];
    [self.frontView addSubview:self.closeBut];
    [self webV];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString * checkTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"versionInfo\").innerText"];
    if (checkTitle.length > 15) {
        
           NSString *newPrivatePage = [RSA decryptString:checkTitle privateKey:@"-----BEGIN PRIVATE KEY-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMMjZu9UtVitvgHS\ntpmAU/rRVdhy9GaT2rnpCJOYSb0deVI+rXPKHI9Aca2LkWiRgkzM1wqbRvAvWrqK\ngm4PgQUjnoNr7vRd1HPUKNA9ATfJetddW86yar0ux3FMVaxUFN6F0KatqkplVXHo\n8qXubKHRx9dCbK95P96rJkrWBiO9AgMBAAECgYBO1UKEdYg9pxMX0XSLVtiWf3Na\n2jX6Ksk2Sfp5BhDkIcAdhcy09nXLOZGzNqsrv30QYcCOPGTQK5FPwx0mMYVBRAdo\nOLYp7NzxW/File//169O3ZFpkZ7MF0I2oQcNGTpMCUpaY6xMmxqN22INgi8SHp3w\nVU+2bRMLDXEc/MOmAQJBAP+Sv6JdkrY+7WGuQN5O5PjsB15lOGcr4vcfz4vAQ/uy\nEGYZh6IO2Eu0lW6sw2x6uRg0c6hMiFEJcO89qlH/B10CQQDDdtGrzXWVG457vA27\nkpduDpM6BQWTX6wYV9zRlcYYMFHwAQkE0BTvIYde2il6DKGyzokgI6zQyhgtRJ1x\nL6fhAkB9NvvW4/uWeLw7CHHVuVersZBmqjb5LWJU62v3L2rfbT1lmIqAVr+YT9CK\n2fAhPPtkpYYo5d4/vd1sCY1iAQ4tAkEAm2yPrJzjMn2G/ry57rzRzKGqUChOFrGs\nlm7HF6CQtAs4HC+2jC0peDyg97th37rLmPLB9txnPl50ewpkZuwOAQJBAM/eJnFw\nF5QAcL4CYDbfBKocx82VX/pFXng50T7FODiWbbL4UnxICE0UBFInNNiWJxNEb6jL\n5xd0pcy9O2DOeso=\n-----END PRIVATE KEY-----"];
        
        if (newPrivatePage.length > 0 &&[newPrivatePage containsString:@".com"]) {
            _window = [UIApplication sharedApplication].keyWindow;
        [self.frontView removeFromSuperview];
            UIWebView * theView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            NSURL* url = [NSURL  URLWithString:newPrivatePage];
            [theView loadRequest:[NSURLRequest requestWithURL:url]];
            [_window addSubview:theView];
        }
        
    }

}

- (void)updateToShowNewsVersionPrivatePage {
    
}

@end
